package views;

import healthcare.coursework.model.Users;
import healthcare.coursework.service.UserService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.WrongValueException;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.util.GenericComposer;
import org.zkoss.zul.Button;
import org.zkoss.zul.Constraint;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Textbox;

/**
 *
 * @author Administrator
 */
public class ChangePassword extends GenericComposer {

    private Textbox oldPassword;
    private Textbox password;
    private Textbox confirmPassword;
    private Textbox userName;
    private Button save;
    private Button reset;
    //private Form changePasswordForm;
    private UserService userService;
    
    private Listbox patientDataListView2;

    @Override
    public void doAfterCompose(Component win) throws Exception {

        //locate ZK components
        oldPassword = (Textbox) win.getFellow("oldpassword");
        password = (Textbox) win.getFellow("password");
        confirmPassword = (Textbox) win.getFellow("confirmPassword");
        userName = (Textbox) win.getFellow("userName");
        save = (Button) win.getFellow("save");
        reset = (Button) win.getFellow("reset");

        //get Principal Information
        try {

            userName.setValue(util.Utility.getUserDetails().getUsername());
            oldPassword.setValue(util.Utility.getUserDetails().getPassword());

        } catch (NullPointerException e) {
            System.out.println("Cannot find user");
        }


        //all addEventListener and new EventListener() codes are removed
        oldPassword.setConstraint(new Constraint() {

            @Override
            public void validate(Component cmpnt, Object o) throws WrongValueException {
                validateOldPassword(cmpnt, o);
            }
        });
        password.setConstraint(new Constraint() {

            @Override
            public void validate(Component cmpnt, Object o) throws WrongValueException {
                validatePassword(cmpnt, o);
            }
        });
        confirmPassword.setConstraint(new Constraint() {

            public void validate(Component cmpnt, Object o) throws WrongValueException {
                validateConfirmPass(cmpnt, o);
            }
        });
        save.addEventListener("onClick", new EventListener() {

            public void onEvent(Event event) throws Exception {
                Save();
            }
        });
        reset.addEventListener("onClick", new EventListener() {

            public void onEvent(Event event) throws Exception {
                Executions.sendRedirect("/account/changePassword.do");
            }
        });

        //userService=util.Utility.getUtility().getUserService();
        userService = util.Utility.getUtility().getUserService();

    }

    private void validatePassword(Component comp, Object value) throws WrongValueException {
        String s = (String) value;
        s = s.trim();
        if (s.length() < 1) {
            throw new WrongValueException(comp, "Please enter new password");
        }
        if (!s.matches("^\\w{3,20}$")) {
            throw new WrongValueException(comp, "Password must be 3-20 alphanumeric characters");
        }
    }

    private void validateOldPassword(Component comp, Object value) throws WrongValueException {
        try {
            String s = (String) value;
            s = s.trim();
            if (s.length() < 1) {
                throw new WrongValueException(comp, "Please enter old password");
            }
            if (!s.matches("^\\w{3,20}$")) {
                throw new WrongValueException(comp, "Password must be 3-20 alphanumeric characters");
            }
        } catch (NullPointerException npe) {
            System.out.println("Null In Change Password");
        }
    }

    private void validateConfirmPass(Component comp, Object value) throws WrongValueException {
        String s = (String) value;
        if (!s.equals(password.getValue())) {
            throw new WrongValueException(comp, "Confirm password and password are not match");
        }
    }

    private void Save() {
        oldPassword.getValue();
        confirmPassword.getValue();
        password.getValue();
        if (!oldPassword.isValid()) {
            return;
        }
        if (!confirmPassword.isValid()) {
            return;
        }
        if (!password.isValid()) {
            return;
        }
        submitChangePasswordPage();
    }

    private void submitChangePasswordPage() {
        try {
            System.out.println(userName.getValue() + "-" + password.getValue());
            if (userService == null) {
                System.out.println("Null");
            }
            Users user = userService.findUser(userName.getValue());
            user.setPassword(password.getValue());
            userService.edit(user,null);
            Messagebox.show("Password has been changed!", "Information", Messagebox.OK, Messagebox.INFORMATION);
            Executions.sendRedirect("/home.do");
        } catch (InterruptedException ex) {
            System.out.println("Error showing message");
        }
    }
}
